layui.define(['layer', 'form', 'laytpl'], function (exports) {
    "use strict";

    let $ = layui.$
        , form = layui.form
        , laypzs = {
        _id: 0
        , config: {}
        , set: function (options) {
            let that = this;
            that.config = $.extend({}, that.config, options);
            return that;
        }
        , on: function (events, callback) {
            return layui.onevent.call(this, 'laypzs', events, callback);
        }
    }
        , thisArea = function () {
        let that = this;
        return {
            laypzs: function (files) {
                that.laypzs.call(that, files);
            }
            , config: that.config
        }
    }
        , Class = function (options) {
        let that = this;
        that.config = $.extend({}, that.config, laypzs.config, options);
        that.render();
    };

    let areaList = {
        pro_list: {
            210000: '辽宁省',
        },
        ZS_list: {
            210100:'',
            210200: '沈阳市',
            210300: '大连市',
            210400: '鞍山市',
            210500: '抚顺市',
            210600: '本溪市',
            210700: '丹东市',
            210800: '锦州市',
            210900: '营口市',
            211000: '阜新市',
            211100: '辽阳市',
            211200: '盘锦市',
            211300: '铁岭市',
            211400: '朝阳市',
            211500: '葫芦岛市',
        },
        ZX_list: {
            210101:'',
            210102: '和平区',
            210103: '沈河区',
            210104: '大东区',
            210105: '皇姑区',
            210106: '铁西区',
            210111: '苏家屯区',
            210112: '浑南区',
            210113: '沈北新区',
            210114: '于洪区',
            210115: '辽中区',
            210123: '康平县',
            210124: '法库县',
            210181: '新民市',
            210190: '经济技术开发区',
            210202: '中山区',
            210203: '西岗区',
            210204: '沙河口区',
            210211: '甘井子区',
            210212: '旅顺口区',
            210213: '金州区',
            210214: '普兰店区',
            210224: '长海县',
            210281: '瓦房店市',
            210283: '庄河市',
            210302: '铁东区',
            210303: '铁西区',
            210304: '立山区',
            210311: '千山区',
            210321: '台安县',
            210323: '岫岩满族自治县',
            210381: '海城市',
            210390: '高新区',
            210402: '新抚区',
            210403: '东洲区',
            210404: '望花区',
            210411: '顺城区',
            210421: '抚顺县',
            210422: '新宾满族自治县',
            210423: '清原满族自治县',
            210502: '平山区',
            210503: '溪湖区',
            210504: '明山区',
            210505: '南芬区',
            210521: '本溪满族自治县',
            210522: '桓仁满族自治县',
            210602: '元宝区',
            210603: '振兴区',
            210604: '振安区',
            210624: '宽甸满族自治县',
            210681: '东港市',
            210682: '凤城市',
            210702: '古塔区',
            210703: '凌河区',
            210711: '太和区',
            210726: '黑山县',
            210727: '义县',
            210781: '凌海市',
            210782: '北镇市',
            210793: '经济技术开发区',
            210802: '站前区',
            210803: '西市区',
            210804: '鲅鱼圈区',
            210811: '老边区',
            210881: '盖州市',
            210882: '大石桥市',
            210902: '海州区',
            210903: '新邱区',
            210904: '太平区',
            210905: '清河门区',
            210911: '细河区',
            210921: '阜新蒙古族自治县',
            210922: '彰武县',
            211002: '白塔区',
            211003: '文圣区',
            211004: '宏伟区',
            211005: '弓长岭区',
            211011: '太子河区',
            211021: '辽阳县',
            211081: '灯塔市',
            211102: '双台子区',
            211103: '兴隆台区',
            211104: '大洼区',
            211122: '盘山县',
            211202: '银州区',
            211204: '清河区',
            211221: '铁岭县',
            211223: '西丰县',
            211224: '昌图县',
            211281: '调兵山市',
            211282: '开原市',
            211302: '双塔区',
            211303: '龙城区',
            211321: '朝阳县',
            211322: '建平县',
            211324: '喀喇沁左翼蒙古族自治县',
            211381: '北票市',
            211382: '凌源市',
            211402: '连山区',
            211403: '龙港区',
            211404: '南票区',
            211421: '绥中县',
            211422: '建昌县',
            211481: '兴城市',
            215090: '工业园区',
        }
    };


    Class.prototype.config = {
        elem: '',
        data: {
            pro: '--选择省--',
            ZS: '--选择市--',
            ZX: '--选择区--',
        },
        change: function(result){}
    };

    Class.prototype.index = 0;

    Class.prototype.render = function () {
        let that = this, options = that.config;
        options.elem = $(options.elem);
        options.bindAction = $(options.bindAction);

        that.events();
    };

    Class.prototype.events = function () {
        let that = this, options = that.config, index;
        let proFilter = 'pro-' + laypzs._id;
        let ZSFilter = 'ZS-' + laypzs._id;
        let ZXFilter = 'ZX-' + laypzs._id;

        let proEl = options.elem.find('.pro-selector');
        let ZSEl = options.elem.find('.ZS-selector');
        let ZXEl = options.elem.find('.ZX-selector');

        //filter
        if(proEl.attr('lay-filter')){
            proFilter = proEl.attr('lay-filter');
        }
        if(ZSEl.attr('lay-filter')){
            ZSFilter = ZSEl.attr('lay-filter');
        }
        if(ZXEl.attr('lay-filter')){
            ZXFilter = ZXEl.attr('lay-filter');
        }
        proEl.attr('lay-filter', proFilter);
        ZSEl.attr('lay-filter', ZSFilter);
        ZXEl.attr('lay-filter', ZXFilter);

        //获取默认值
        if(proEl.data('value')){
            options.data.pro = proEl.data('value');
        }
        if(ZSEl.data('value')){
            options.data.ZS = ZSEl.data('value');
        }
        if(ZXEl.data('value')){
            options.data.ZX = ZXEl.data('value');
        }
        proEl.attr('lay-filter', proFilter);
        ZSEl.attr('lay-filter', ZSFilter);
        ZXEl.attr('lay-filter', ZXFilter);

        //监听结果
        form.on('select('+proFilter+')', function(data){
            options.data.pro = data.value;
            let code = getCode('pro', data.value);
            renderZS(code);

            options.change(options.data);
        });
        form.on('select('+ZSFilter+')', function(data){
            options.data.ZS = data.value;
            let code = getCode('ZS', data.value);
            renderZX(code);

            options.change(options.data);
        });
        form.on('select('+ZXFilter+')', function(data){
            options.data.ZX = data.value;

            options.change(options.data);
        });

        renderPro();

        //查找pro
        function renderPro(){
            let tpl = '';
            let proList = getList("pro");
            let currentCode = '';
            let currentName = '';
            proList.forEach(function(_item){
                if (!currentCode){
                    currentCode = _item.code;
                    currentName = _item.name;
                }
                if(_item.name === options.data.pro){
                    currentCode = _item.code;
                    currentName = _item.name;
                }
                tpl += '<option value="'+_item.name+'">'+_item.name+'</option>';
            });
            options.data.pro = currentName;
            proEl.html(tpl);
            proEl.val(options.data.pro);
            form.render('select');
            renderZS(currentCode);
        }

        function renderZS(proCode){
            let tpl = '';
            let ZSList = getList('ZS', proCode.slice(0, 2));
            let currentCode = '';
            let currentName = '';
            ZSList.forEach(function(_item){
                if (!currentCode){
                    currentCode = _item.code;
                    currentName = _item.name;
                }
                if(_item.name === options.data.ZS){
                    currentCode = _item.code;
                    currentName = _item.name;
                }
                tpl += '<option value="'+_item.name+'">'+_item.name+'</option>';
            });
            options.data.ZS = currentName;
            ZSEl.html(tpl);
            ZSEl.val(options.data.ZS);
            form.render('select');
            renderZX(currentCode);
        }

        function renderZX(ZSCode){
            let tpl = '';
            let ZXList = getList('ZX', ZSCode.slice(0, 4));
            let currentCode = '';
            let currentName = '';
            ZXList.forEach(function(_item){
                if (!currentCode){
                    currentCode = _item.code;
                    currentName = _item.name;
                }
                if(_item.name === options.data.ZX){
                    currentCode = _item.code;
                    currentName = _item.name;
                }
                tpl += '<option value="'+_item.name+'">'+_item.name+'</option>';
            });
            options.data.ZX = currentName;
            ZXEl.html(tpl);
            ZXEl.val(options.data.ZX);

            form.render('select');
        }

        function getList(type, code) {
            let result = [];

            if (type !== 'pro' && !code) {
                return result;
            }

            let list = areaList[type + "_list"] || {};
            result = Object.keys(list).map(function (code) {
                return {
                    code: code,
                    name: list[code]
                };
            });

            if (code) {
                // oversea code
                if (code[0] === '9' && type === 'ZS') {
                    code = '9';
                }

                result = result.filter(function (item) {
                    return item.code.indexOf(code) === 0;
                });
            }

            return result;
        }

        function getCode(type, name){
            let code = '';
            let list = areaList[type + "_list"] || {};
            layui.each(list, function(_code, _name){
                if(_name === name){
                    code = _code;
                }
            });

            return code;
        }
    };

    laypzs.render = function (options) {
        let inst = new Class(options);
        laypzs._id++;
        return thisArea.call(inst);
    };

    //暴露接口
    exports('laypzs', laypzs);
});